###############################################
#                                             #
#     National Survey Treatment Effects       #
#           Using all respondents             #
#     Created by Mac Lockhart Nov 30 2022     #
#     Updated Nov 30 2022                     #
#                                             #
###############################################

library(tidyverse)
library(data.table)
library(modelsummary)
library(kableExtra)
library(gt)
library(flextable)
library(car)

#Read in most recent version of cleaned data using:
#Clean_National_Sample.R
if(Sys.info()[7] == "maclockhart"){
  setwd("/Users/maclockhart/Dropbox/School/RA Work/'22 Seth & Thad/MIT Evolving Election Administration Landscape")
}

DF <- fread("Survey Data/National Recoded.csv")
CO <- fread("Survey Data/Colorado Recoded.csv")
GA <- fread("Survey Data/Georgia Recoded.csv")
LA <- fread("Survey Data/LA Recoded.csv")
TX <- fread("Survey Data/Texas Recoded.csv")

DF$bot <- 0
DF$n<-1:nrow(DF)
#assign sample names
DF$sample<-"USA"
CO$sample<-"CO"
GA$sample<-"GA"
LA$sample <- "LA"
TX$sample <-"TX"

#Remove bots
DF<-rbindlist(list(DF, CO, GA, LA, TX), fill=T)
rm(CO, GA, LA, TX)
DF<-DF[bot==0]

#Recode outcomes as change from pre-treatment values following the pre-analysis plan
DF$ownstate <- DF$ownstatepost-DF$ownstatepre
DF$otherstates <- DF$otherstatepost - DF$otherstatepre
DF$votefraud <- DF$votefraudpost - DF$votefraudpre
DF$officialfraud <- DF$officialfraudpost - DF$officialfraudpre
DF$vote2024 <- DF$vote2024post - DF$vote2024pre

#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment, DF)
m1.2 <- lm(otherstates ~ d_treatment, DF)
m1.3 <- lm(votefraud ~ d_treatment, DF)
m1.4 <- lm(officialfraud ~ d_treatment, DF)
m1.5 <- lm(vote2024 ~ d_treatment, DF)

#Separate treatment effects by video
m2.1 <- lm(ownstate ~ treatment, DF)
m2.2 <- lm(otherstates ~ treatment, DF)
m2.3 <- lm(votefraud ~ treatment, DF)
m2.4 <- lm(officialfraud ~ treatment, DF)
m2.5 <- lm(vote2024 ~ treatment, DF)

binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/national_binary.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
full_models <- list("Trust Own State" = m2.1,
                      "Trust Other States" = m2.2,
                      "Vote Fraud Belief" = m2.3,
                      "Officials Fraud Belief" = m2.4,
                      "2024 Vote Intent" = m2.5)
modelsummary(full_models, output = "Mac and Jen Results/Treatment Effects/national_categorical.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
rm(m1.1, m1.2, m1.3, m1.4, m1.5, m2.1, m2.2, m2.3, m2.4, m2.5)

hypothesis <- c("treatmentEmotions = treatmentFacts")
linearHypothesis(m2.1, hypothesis)
linearHypothesis(m2.2, hypothesis)
linearHypothesis(m2.3, hypothesis)
linearHypothesis(m2.4, hypothesis)
linearHypothesis(m2.5, hypothesis)



##Treatment effects by party
#Pooled treatment effects by video
m1.1 <- lm(ownstate ~ d_treatment*party, DF)
m1.2 <- lm(otherstates ~ d_treatment*party, DF)
m1.3 <- lm(votefraud ~ d_treatment*party, DF)
m1.4 <- lm(officialfraud ~ d_treatment*party, DF)
m1.5 <- lm(vote2024 ~ d_treatment*party, DF)


binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/national_binary_byparty.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('d_treatment' = 'Treated',
                          "partyIndependent"="Independent",
                          "partyRepublican" = "Republican",
                          "d_treatment:partyIndependent" = "Treated X Independent",
                          "d_treatment:partyRepublican" = "Treated X Republican"),
             #coef_omit = "Intercept", 
             gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
rm(m1.1, m1.2, m1.3, m1.4, m1.5)


#Subnational treatments pooled
DF$anystate_treatment <- ifelse(DF$state_treatment=="Control", 0, 1)
m1.1 <- lm(ownstate ~ anystate_treatment, DF, weights = the.wts)
m1.2 <- lm(otherstates ~ anystate_treatment, DF, weights = the.wts)
m1.3 <- lm(votefraud ~ anystate_treatment, DF, weights = the.wts)
m1.4 <- lm(officialfraud ~ anystate_treatment, DF, weights = the.wts)
m1.5 <- lm(vote2024 ~ anystate_treatment, DF, weights = the.wts)

m2.1 <- lm(ownstate ~ anystate_treatment + as.factor(sample), DF, weights = the.wts)
m2.2 <- lm(otherstates ~ anystate_treatment+ as.factor(sample), DF, weights = the.wts)
m2.3 <- lm(votefraud ~ anystate_treatment+ as.factor(sample), DF, weights = the.wts)
m2.4 <- lm(officialfraud ~ anystate_treatment+ as.factor(sample), DF, weights = the.wts)
m2.5 <- lm(vote2024 ~ anystate_treatment+ as.factor(sample), DF, weights = the.wts)

binary_models <- list("Trust Own State" = m1.1,
                      "Trust Other States" = m1.2,
                      "Vote Fraud Belief" = m1.3,
                      "Officials Fraud Belief" = m1.4,
                      "2024 Vote Intent" = m1.5)
modelsummary(binary_models, output = "Mac and Jen Results/Treatment Effects/states_pooled.docx",
             #estimate  = "{estimate}{stars} [{conf.low}, {conf.high}]",
             statistic = 'std.error', stars=T,
             coef_map = c('anystate_treatment' = 'Treated'),
             coef_omit = "Intercept", gof_omit = 'DF|Deviance|Log.Lik.|AIC|BIC|RMSE|R2 Adj.')
rm(m1.1, m1.2, m1.3, m1.4, m1.5)
